package com.sgcc.fj.pmos.e_fj_order.activity;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.arouter.app.AppArouterPath;
import com.sgcc.fj.pmos.arouter.order.OrderArouterPath;
import com.sgcc.fj.pmos.common.base.MvpBaseActivity;
import com.sgcc.fj.pmos.common.utils.DateUtils;
import com.sgcc.fj.pmos.common.utils.DialogUtil;
import com.sgcc.ln.pmos.common.widget.TitleBar;
import com.sgcc.fj.pmos.e_fj_order.adapter.AddSupplementContractItemAdapter;
import com.sgcc.fj.pmos.e_fj_order.bean.AddSupplementContractItemBean;
import com.sgcc.fj.pmos.e_fj_order.bean.PackageOrdersBean;
import com.sgcc.fj.pmos.e_fj_order.contract.AddSupplementContract;
import com.sgcc.fj.pmos.e_fj_order.presenter.AddSupplementContractPresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ChaoAJ
 * @describe 新增补充协议
 * @time 2022/11/8 11:36
 */
@Route(path = OrderArouterPath.ORDER_ADDSUPPLEMENTCONTRACT)
public class AddSupplementContractActivity extends MvpBaseActivity<AddSupplementContractPresenter> implements AddSupplementContract.View, View.OnClickListener {

    private TitleBar tbTitleBar;
    private RecyclerView rvDataList;
    private TextView tvSubmit;

    private LinearLayout llUpdate;
    private LinearLayout llFileDate;
    private TextView tvFileName;
    private ImageView imgClose;

    @Autowired
    public String relationId;
    private AddSupplementContractItemAdapter addSupplementContractItemAdapter;

    private String fileId;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_add_supplement_contract;
    }


    @Override
    protected AddSupplementContractPresenter createPresenter() {
        return new AddSupplementContractPresenter();
    }

    @Override
    protected void initViews() {
        super.initViews();
        tbTitleBar = findViewById(R.id.tbTitleBar);
        rvDataList = findViewById(R.id.rvDataList);
        tvSubmit = findViewById(R.id.tvSubmit);

        llUpdate = (LinearLayout) findViewById(R.id.llUpdate);
        llFileDate = (LinearLayout) findViewById(R.id.llFileDate);
        tvFileName = (TextView) findViewById(R.id.tvFileName);
        imgClose = (ImageView) findViewById(R.id.imgClose);
        rvDataList = (RecyclerView) findViewById(R.id.rvDataList);

        llUpdate.setOnClickListener(this);
        tvSubmit.setOnClickListener(this);
        imgClose.setOnClickListener(this);

    }

    @Override
    protected void initData() {
        super.initData();
        tbTitleBar.setTitle("新增补充协议");
        tbTitleBar.setRightIcon(R.mipmap.go_main, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goMainActivity();
            }
        });
        rvDataList.setLayoutManager(new LinearLayoutManager(this));
        addSupplementContractItemAdapter = new AddSupplementContractItemAdapter();
        rvDataList.setAdapter(addSupplementContractItemAdapter);
        mPresenter.getPackageOrders(relationId);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.llUpdate) {
            ARouter.getInstance()
                    .build(AppArouterPath.APP_CHOOSEFILE)
                    .navigation(AddSupplementContractActivity.this, 0x124);

        }

        if (view.getId() == R.id.imgClose) {
            fileId = "";
            llUpdate.setVisibility(View.VISIBLE);
            llFileDate.setVisibility(View.GONE);
        }
        if (view.getId() == R.id.tvSubmit) {//提交
            if (TextUtils.isEmpty(fileId)) {
                ToastUtils.showLong("请选择备案补充协议");
                return;
            }


            DialogUtil.showConfirm(mContext, "提示", "是否确认提交补充协议？", new OnConfirmListener() {
                @Override
                public void onConfirm() {
                    Map<String, String> map = new HashMap<>();

                    List<AddSupplementContractItemBean> data = addSupplementContractItemAdapter.getData();
                    for (AddSupplementContractItemBean datum : data) {
                        map.put(datum.getMonth1(), datum.getValue());
                    }


                    mPresenter.addSupplementContract(fileId, GsonUtils.toJson(map), relationId);
                }
            });

        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 2222) {
            if (requestCode == 0x124) {
                String fileName = data.getStringExtra("FileName");
                String filePath = data.getStringExtra("FilePath");

                llUpdate.setVisibility(View.GONE);
                llFileDate.setVisibility(View.VISIBLE);
                tvFileName.setText(fileName);
                mPresenter.upload(filePath);
            }
        }
    }


    @Override
    public void addSupplementContractSuccess() {
        ToastUtils.showLong("备案补充协议保存成功");
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void getPackageOrdersSuccess(List<PackageOrdersBean.DataBean> list) {
        List<AddSupplementContractItemBean> dataList = new ArrayList<>();
        for (PackageOrdersBean.DataBean dataBean : list) {
            dataList.add(new AddSupplementContractItemBean(
                    DateUtils.getStrng_to_Times_ymd_6(dataBean.getMonthDate()),
                    DateUtils.getStrng_to_Times_ymd_3(dataBean.getMonthDate())));
        }
        addSupplementContractItemAdapter.setList(dataList);
    }

    @Override
    public void addSupplementContractFailed() {

    }

    @Override
    public void uploadSuccess(String fileId) {
        this.fileId = fileId;
        ToastUtils.showLong("文件上传成功");

    }

    @Override
    public void uploadFailed() {
        fileId = "";
        llUpdate.setVisibility(View.VISIBLE);
        llFileDate.setVisibility(View.GONE);

    }


}